package com.ft.oa.service.development.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.development.ProductVersion;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.development.ProductVersionMapper;
import com.ft.oa.service.development.ProductVersionService;
import com.ft.oa.service.sys.SysUserService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductVersionServiceImpl implements ProductVersionService {

    @Resource
    private ProductVersionMapper productVersionMapper;
    @Resource
    private SysUserService sysUserService;


    @Override
    public List<ProductVersion> list(ProductVersion condition) {

        QueryWrapper<ProductVersion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ProductVersion::getProductId, condition.getProductId())
                .eq(ProductVersion::getState, 0)
                .eq(ProductVersion::getCategory, 1);

        List<ProductVersion> productVersions = productVersionMapper.selectList(wrapper);

        return productVersions;
    }

    @Override
    public List<SysUser> versionChiefList() {
        return sysUserService.listAll();
    }

    /**
     *  根据产品id查看版本线列表
     * @param
     * @param
     * @return
     */
    @Override
    public Page<ProductVersion> page(ProductVersion version) {


        QueryWrapper<ProductVersion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), ProductVersion::getCreator ,SecurityUtils.getCurrentUser().getId())
                .eq(ProductVersion::getProductId, version.getProductId())
                .eq(ProductVersion::getState, 0)
                .ne(ProductVersion::getCategory, 1)
                .eq(version.getCategory() != null, ProductVersion::getCategory, version.getCategory())
                .like(StringUtils.hasText(version.getChief()), ProductVersion::getChief, version.getChief())
                .ge(version.getProjectTimeLeft() != null, ProductVersion::getProjectTime, version.getProjectTimeLeft())
                .le(version.getProjectTimeRight() != null, ProductVersion::getProjectTime, version.getProjectTimeRight())
                .ge(version.getPracticalTimeLeft() != null, ProductVersion::getPracticalTime, version.getPracticalTimeLeft())
                .le(version.getPracticalTimeRight() != null, ProductVersion::getPracticalTime, version.getPracticalTimeRight());

        Page<ProductVersion> pages = new Page<>(version.getPage(),version.getSize());
        Page<ProductVersion> result = productVersionMapper.selectPage(pages, wrapper);
        return result;
    }


    @Override
    @Transactional
    public void add(ProductVersion productVersion) {

        productVersionMapper.insert(productVersion);
    }

    @Override
    @Transactional
    public void edit(ProductVersion version) {
        LambdaUpdateChainWrapper<ProductVersion> wrapper = new LambdaUpdateChainWrapper<>(productVersionMapper);
        boolean update = wrapper.eq(ProductVersion::getId, version.getId())
                .set(version.getCategory() != null, ProductVersion::getCategory, version.getCategory())
                .set(StringUtils.hasText(version.getBaseVersions()), ProductVersion::getBaseVersions, version.getBaseVersions())
                .set(StringUtils.hasText(version.getProductVersions()), ProductVersion::getProductVersions, version.getProductVersions())
                .set(StringUtils.hasText(version.getDemand()), ProductVersion::getDemand, version.getDemand())
                .set(StringUtils.hasText(version.getInfo()), ProductVersion::getInfo, version.getInfo())
                .set(version.getChiefId() != null, ProductVersion::getChiefId, version.getChiefId())
                .set(StringUtils.hasText(version.getChief()), ProductVersion::getChief, version.getChief())
                .set(version.getProjectTime() != null,ProductVersion::getProjectTime, version.getProjectTime())
                .set(version.getAccomplishTime() != null,ProductVersion::getAccomplishTime, version.getAccomplishTime())
                .set(version.getPracticalTime() != null,ProductVersion::getPracticalTime, version.getPracticalTime())
                .update();
    }

    /**
     * 删除版本线信息
     *
     * @return
     */
    @Override
    @Transactional
    public void del(ProductVersion version) {
        LambdaUpdateChainWrapper<ProductVersion> wrapper = new LambdaUpdateChainWrapper<>(productVersionMapper);
        boolean update = wrapper.eq(ProductVersion::getId, version.getId())
                .set(ProductVersion::getState, 1)
                .update();
    }
}
